﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;

public partial class Main : Permission
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 一级菜单
            Bind_Menu();

            // 登录用户姓名
            ltUser.Text = Server.UrlDecode(CookieManager.RealName);
            // 店铺名
            ltSupperGuideName.Text = Server.UrlDecode(CookieManager.CIIDES);
        }
    }

    /// <summary>
    /// 一级菜单
    /// </summary>
    public void Bind_Menu()
    {
        rptMenu.DataSource = new WMS.BLL.OrderOnline_Model().GetByPermission(CookieManager.RolePermission);
        rptMenu.DataBind();
    }

    protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Literal lt = e.Item.FindControl("ltModelID") as Literal;

            StringBuilder tree = new StringBuilder();
            tree.Append("<ul class='submenu'>");
            List<WMS.Model.MainMenuTree> mainMenuTree = new WMS.BLL.OrderOnline_Right().GetAllChildrenByModelID(lt.Text);
            foreach (WMS.Model.MainMenuTree mainMenu in mainMenuTree)
            {
                if (CookieManager.RolePermission.IndexOf(mainMenu.id) != -1)
                {
                    RenderTree(tree, mainMenu);
                }
            }
            tree.Append("</ul>");

            lt.Text = tree.ToString();
        }
    }

    public void RenderTree(StringBuilder tree, WMS.Model.MainMenuTree mainMenu)
    {
        if (mainMenu.children.Count == 0)
        {
            tree.AppendFormat("<li><span><a onclick=\"addTab('{0}','{1}','')\">{0}</a></span></li>", mainMenu.text, mainMenu.attributes);
        }
        else
        {
            tree.Append("<li state='closed'>");
            tree.AppendFormat("<span>{0}</span>", mainMenu.text);
            tree.Append("<ul>");
            foreach (WMS.Model.MainMenuTree node in mainMenu.children)
            {
                if (CookieManager.RolePermission.IndexOf(node.id) != -1)
                {
                    RenderTree(tree, node);
                }
            }
            tree.Append("</ul>");
            tree.Append("</li>");
        }
    }
}